package com.bw.liuyushan.zhuangao4_mvptest1.MVP.View.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.WindowManager;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.ToastUtils;
import com.bw.liuyushan.mvp_lib.View.BaseActivity;
import com.bw.liuyushan.zhuangao4_mvptest1.Adapter.UserAdapter;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.Contrack.ICTest;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.Model.BaseModelTest;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.Presenter.BasePresenterTest;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.View.Fragment.logFragment;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.View.Fragment.regFragment;
import com.bw.liuyushan.zhuangao4_mvptest1.R;
import com.bw.liuyushan.zhuangao4_mvptest1.Utils.WindowUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;

/**
 * ZhuanGao4_MVPTest1
 * file name is : UserActivity
 * created by Ender on 2021/8/19 15:00.
 * author : 刘雨杉
 * Describe(作用):登陆和注册的Fragment所属的Activity 接收EventBus切换Fragment 传值跳转
 */
public class UserActivity extends BaseActivity<BasePresenterTest> implements ICTest.C_ViewTest {
    private androidx.viewpager.widget.ViewPager vpUser;
    ArrayList<Fragment> list_User;//Fragment数组

    @Override
    public int bindLayout() {
        return R.layout.activity_user;
    }

    @Override
    public void initView() {
        //沉浸式窗口
        WindowUtils.hideStatusBar(this);
        //EventBus注册
        EventBus.getDefault().register(this);

        //防止内存泄露
        ip=new BasePresenterTest(new BaseModelTest(), this);
        vpUser = (ViewPager) findViewById(R.id.vp_User);
    }

    @Override
    public void initDate() {

        list_User = new ArrayList<>();
        list_User.add(new regFragment());
        list_User.add(new logFragment());

        UserAdapter userAdapter = new UserAdapter(getSupportFragmentManager(), list_User);
        vpUser.setAdapter(userAdapter);
//        SharedPreferences sp = getSharedPreferences("sp", MODE_PRIVATE);
//        boolean first = sp.getBoolean("first", true);
//        if (first){
//
//        }else{
//            ToastUtils.showShort("已登录,欢迎再次进入app");
////            Intent intent = new Intent(this, IndexActivity.class);
////            startActivity(intent);
//        }



    }

    /**
     * 接收到regFragment发送信息->切换Fragment->到登录页面
     * @param s
     */
    @Subscribe(sticky = true)//EventBus的方法 Observer提供的订阅
    public void Evnb(String s) {
        if (s.equals("注册成功")) {
            vpUser.setCurrentItem(1);
        }
    }

    /**
     * 接收到regFragment发送的信息->切换Fragment->到登陆的logFragment
     * @param ss
     */
    @Subscribe(sticky = true)//EventBus的方法 Observer提供的订阅
    public void onLogin (String ss){
        if (ss.equals("直接登录")) {
                vpUser.setCurrentItem(1);
        }
    }

    /**
     * 窗口属性更改的监听事件->调用沉浸式窗口的工具类
     * @param params
     */
    @Override
    public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
        super.onWindowAttributesChanged(params);
        WindowUtils.hideStatusBar(this);
    }

    @Override
    protected void onDestroy () {
        super.onDestroy();
        //EventBus注销
        EventBus.getDefault().unregister(this);
    }


}
